package com.jvm.visualvm;

// 死锁例子
public class Demo1 {

    public static void main(String[] args) {
        Obj1 obj1 = new Obj1();
        Obj2 obj2 = new Obj2();

        Thread thread1 =
                new Thread(new SynAddRunnable(obj1, obj2, 1, 2, true));
        thread1.setName("thread1");
        thread1.start();
        Thread thread2 =
                new Thread(new SynAddRunnable(obj1, obj2, 2, 1, false));
        thread2.setName("thread2");
        thread2.start();

    }


    // 线程死锁等待延时
    public static class SynAddRunnable implements Runnable {
        Obj1 obj1;
        Obj2 obj2;
        int a, b;

        boolean flag;

        public SynAddRunnable(Obj1 obj1, Obj2 obj2, int a, int b, boolean flag) {
            this.obj1 = obj1;
            this.obj2 = obj2;
            this.a = a;
            this.b = b;
            this.flag = flag;
        }

        /**
         *
         * jps 查看java进程
         * jstack 9808  查看死锁状态
         */
        @Override
        public void run() {
            try {
                if (flag) {
                    synchronized (obj1) {
                        Thread.sleep(1000);
                        synchronized (obj2) {
                            System.out.println(a + b);
                        }
                    }
                } else {
                    synchronized (obj2) {
                        Thread.sleep(1000);
                        synchronized (obj1) {
                            System.out.println(a + b);
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static class Obj1 {
        //
    }

    public static class Obj2 {
        //
    }

}
